import {mapGetters} from 'vuex';
import globalHelper from "@/utils/globalHelper";

export default {
    data() {
        return {
            filters: {
                type: '',
                caseInfo: ''
            },
            riskReasonTypeOptions: [
                {id: 0, name: '电流和电压原因'},
                {id: 1, name: '温度和湿度原因'}
            ],
            iptIdOptions: [],
            thoIdOptions: [],
            addForm: {},
            addFormVisible: false,
            editForm: {},
            editFormVisible: false,
            addFormRules: {
                upperScore: [
                    {required: true, message: "请输入上限报警分数", trigger: "blur"}
                ],
                lowerScore: [
                    {required: true, message: "请输入下限报警分数", trigger: "blur"}
                ],
                reason: [
                    {required: true, message: "请输入原因", trigger: "blur"}
                ],
                measure: [
                    {required: true, message: "请输入措施", trigger: "blur"}
                ],
                type: [
                    {required: true, message: '请选择原因类型', trigger: 'change'}
                ]
            },
            editFormRules: {
                upperScore: [
                    {required: true, message: "请输入上限报警分数", trigger: "blur"}
                ],
                lowerScore: [
                    {required: true, message: "请输入下限报警分数", trigger: "blur"}
                ],
                reason: [
                    {required: true, message: "请输入原因", trigger: "blur"}
                ],
                measure: [
                    {required: true, message: "请输入措施", trigger: "blur"}
                ],
                type: [
                    {required: true, message: '请选择原因类型', trigger: 'change'}
                ]
            },
        }
    },
    computed: {
        ...mapGetters([
            'riskReasonList',
            'riskReasonPage'
        ])
    },
    watch: {},
    components: {},
    mounted() {

        this.getAllDataOpt();
    },
    methods: {
        getAllDataOpt: function () {
            this.$store.dispatch('getRiskReasonList', {...this.filters});
        },

        handleCurrentChange(val) {
            this.riskReasonPage.page = val - 1;
            this.getAllDataOpt();
        },
        handleSizeChange(val) {
            this.riskReasonPage.page = 0;
            this.riskReasonPage.rows = val;
            this.getAllDataOpt();
        },
        handleSearch() {
            this.riskReasonPage.page = 0;
            this.getAllDataOpt();
        },
        tableOperate(typeOpt, row) {
            if (typeOpt === 'edit') {
                this.editForm = Object.assign({}, row);
                this.editFormVisible = true;
            } else if (typeOpt === 'add') {
                this.addForm = {};
                this.addFormVisible = true;
            } else if (typeOpt === 'del') {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$store.dispatch('deleteRiskReason', {id: row.id}).then((res) => {
                        this.getAllDataOpt();
                    });
                });
            }
        },
        addFormSubmit: function () {
            globalHelper.formValidate(this, 'addForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.addForm);
                    this.$store.dispatch('addRiskReason', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '创建风险原因成功'
                        });
                        this.getAllDataOpt();
                        this.addFormVisible = false;
                    });
                }
            });


        },
        editFormSubmit: function () {
            globalHelper.formValidate(this, 'addForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.editForm);
                    this.$store.dispatch('editRiskReason', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '修改风险原因成功'
                        });
                        this.getAllDataOpt();
                        this.editFormVisible = false;
                    });
                }
            });


        },
    }
}
